package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.BussGoodsTypeService;
import com.baichang.taoshunda.service.BussTypeService;
import com.baichang.taoshunda.service.CityService;
import com.baichang.taoshunda.service.WaiterTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/8/24 0024.
 */
@Controller
@RequestMapping(ControllerConstants.TYPE)
public class TypeManagementWController extends WebBaseController {

    @Autowired
    private BussTypeService bussTypeService;

    @Autowired
    private WaiterTypeService waiterTypeService;

    @Autowired
    private CityService cityService;

    /**
     * 跳转首页分类列表
     *
     * @return
     */
    @RequestMapping("/findHomePageTypeIndex")
    public String findHomePageType() {
        return "taoshunda/type/homePageType";
    }

    /**
     * 跳转本地生活列表
     *
     * @return
     */
    @RequestMapping("/findLifeTypeIndex")
    public String findLifeType() {
        return "taoshunda/type/lifeServiceType";
    }

    /**
     * 查询首页推荐
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryHomePage")
    public void queryHomePage(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                return bussTypeService.queryHomePage(name, nowPage, pageSize);
            }
        });
    }

    /**
     * 添加首页分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addHomePageType")
    public void addHomePageType(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer cityId = getInt(request, "cityId", 0);
                Integer rank = getInt(request, "rank", 0);
                String typeName = getString(request, "typeName", "");
                String typePic = getString(request, "typePic", "");
                BigDecimal price = BigDecimal.valueOf(getDouble(request, "price", 0));
                BigDecimal money = BigDecimal.valueOf(getDouble(request, "money", 0));
                String proportionate = getString(request, "proportionate", "");
                return bussTypeService.addHomePageType(cityId, rank, typeName, typePic, price, money, proportionate);
            }
        });
    }

    /**
     * 修改首页分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateHomePageType")
    public void updateHomePageType(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer rank = getInt(request, "rank", 0);
                Integer cityId = getInt(request, "cityId", 0);
                String typeName = getString(request, "typeName", "");
                String typePic = getString(request, "typePic", "");
                BigDecimal price = BigDecimal.valueOf(getDouble(request, "price", 0));
                BigDecimal money = BigDecimal.valueOf(getDouble(request, "money", 0));
                String proportionate = getString(request, "proportionate", "");
                return bussTypeService.updateHomePageType(id, rank, cityId, typeName, typePic, price, money, proportionate);
            }
        });
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateState")
    public void updateState(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer state = getInt(request, "state", 0);
                return bussTypeService.updateState(id, state);
            }
        });
    }

    /**
     * 获取城市信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryByCityId")
    public void queryByCityId(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer cityId = getInt(request, "cityId", 0);
                Integer deptPath = getInt(request, "deptPath", 0);
                return cityService.queryByCityId(cityId, deptPath);
            }
        });
    }

    /**
     * 查询本地服务
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryLifeType")
    public void queryLifeType(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                return waiterTypeService.queryLifeType(name, nowPage, pageSize);
            }
        });
    }

    /**
     * 添加生活服务列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addLifeType")
    private void addWaiterLifeType(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer isSpecial = getInt(request, "isSpecial", 0);
                Integer sorting = getInt(request, "sorting", 0);
                String name = getString(request, "waiterName", "");
                String pic = getString(request, "waiterPic", "");
                BigDecimal price = BigDecimal.valueOf(getDouble(request, "price", 0));
                return waiterTypeService.addWaiterLifeType(isSpecial, sorting, name, pic, price);
            }
        });
    }

    /**
     * 修改生活列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateWaiterLifeType")
    private void updateWaiterLifeType(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer sorting = getInt(request, "sorting", 0);
                Integer isSpecial = getInt(request, "isSpecial", 1);
                String name = getString(request, "waiterName", "");
                String pic = getString(request, "waiterPic", "");
                String price = getString(request, "price", "");
                return waiterTypeService.updateWaiterLifeType(id, sorting, isSpecial, name, pic,price);
            }
        });
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateLifeState")
    public void updateLifeState(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer state = getInt(request, "state", 0);
                return waiterTypeService.updateLifeState(id, state);
            }
        });
    }

    /**
     * 首页推荐
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateIsHome")
    public void updateIsHome(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer isHome = getInt(request, "isHome", 0);
                return waiterTypeService.updateIsHome(id, isHome);
            }
        });
    }
}
